﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Data;

namespace Sync.Library
{
    [Serializable]
    public class Table:Csla.BusinessBase<Table>
    {
        string name = "";
        string remoteName = "";
        Columns columns = new Columns();

        private Server server;
        public Server Server
        {
            get { return server; }
            set { server = value; }
        }

        private bool selected = false;
        public bool Selected
        {
            get { return selected; }
            set 
            { 
                selected = value;

                if (server != null)
                {
                    /*if (selected == true)
                    {
                        if((from i in server.Tables where i.Name == name select i).Any() == false)
                        {
                            Table t = new Table();
                            t.Name = name;
                            t.remoteName = remoteName;
                            t.selected = selected;
                            server.Tables.Add(t);
                        }
                    }
                    else
                    {
                        if (server.Tables.Count > 0)
                        {
                            if ((from i in server.Tables where i.Name == name select i).Any() == true)
                            {
                                server.Tables.Remove((from i in server.Tables where i.Name == name select i).First());
                            }
                        }
                    }*/
                }
            }
        }

        private int object_id;

        public int Object_id
        {
            get { return object_id; }
            set { object_id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string RemoteName
        {
            get { return remoteName; }
            set { remoteName = value; }
        }

        public Columns Columns
        {
            get { return columns; }
            set { columns = value; }
        }

        private Table()
        {
            columns = new Columns();
            MarkAsChild();
        }

        public Table(string name)
        {
            columns = new Columns();
            MarkAsChild();
        }

        public static Table GetTable(Csla.Data.SafeDataReader dr)
        {
            Table t = new Table();
            t.Fetch(dr);
            return t;
        }

        private void Fetch(Csla.Data.SafeDataReader dr)
        {
            object_id = dr.GetInt32(0);
            name = dr.GetString(1);
        }
    }
}
